/**
 *
 */
package com.yangtao.framework.hibernate.entity;

import com.yangtao.framework.hibernate.BaseEntity;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.*;
import java.util.List;

/**
 * 组织机构对象
 *
 * @author 杨涛
 */
@Entity
@Table(name = "sys_organization")
public class Organization extends BaseEntity {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 机构编号
	 */
	@Column(length = 20)
	private String orgCode;
	/**
	 * 组织log标志
	 */
	@Column(length = 20)
	private String logo;
	/**
	 * 机构层次
	 */
	@Column(length = 2)
	private Integer level;
	/**
	 * 路径,专为树状结构使用
	 */
	@Column(columnDefinition = "varchar(500)")
	private String path;

	/**
	 * 机构名称
	 */
	@Column(length = 200)
	private String name;
	/**
	 * 机构类型
	 */
	@Column(length = 20)
	private String type;
	/**
	 * 机构描述
	 */
	private String desription;
	/**
	 * 组织状态<br>
	 * <启用<1>，禁用<0>>
	 */
	@Column(length = 1)
	private Boolean active;

	/**
	 * 父节点ID
	 */
	@Column(name = "supId", length = 20, updatable = false, insertable = false)
	private String parentId;

	/**
	 * 父机构
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "supId")
	@Cascade(value = {CascadeType.SAVE_UPDATE})
	private Organization supOrganization;
	/**
	 * 机构所含角色
	 */
	@OneToMany(mappedBy = "organization", fetch = FetchType.LAZY)
	@Cascade({CascadeType.DELETE})
	private List<Role> roles;
	/**
	 * 机构所含用户
	 */
	@OneToMany(mappedBy = "organization", fetch = FetchType.LAZY)
	@Cascade({CascadeType.DELETE})
	private List<User> users;
	/**
	 * 子机构
	 */
	@OneToMany(mappedBy = "supOrganization", fetch = FetchType.LAZY)
	@Cascade({CascadeType.DELETE})
	private List<Organization> subOrganization;

	/**
	 * 判断是否有子组织机构
	 *
	 * @return
	 */
	public boolean hasChildren() {
		if (this.subOrganization != null && this.subOrganization.size() > 0)
			return true;
		return false;
	}

	/**
	 * 判断组织机构是否有用户
	 *
	 * @return
	 */
	public boolean hasUser() {
		if (this.users != null && this.users.size() > 0)
			return true;
		return false;
	}

	public boolean hasRole() {
		if (this.roles != null && this.roles.size() > 0)
			return true;
		return false;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the level
	 */
	public Integer getLevel() {
		return level;
	}

	/**
	 * @param level the level to set
	 */
	public void setLevel(Integer level) {
		this.level = level;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @return the logo
	 */
	public String getLogo() {
		return logo;
	}

	/**
	 * @param logo the logo to set
	 */
	public void setLogo(String logo) {
		this.logo = logo;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the desription
	 */
	public String getDesription() {
		return desription;
	}

	/**
	 * @param desription the desription to set
	 */
	public void setDesription(String desription) {
		this.desription = desription;
	}

	/**
	 * @return the supOrganization
	 */
	public Organization getSupOrganization() {
		return supOrganization;
	}

	/**
	 * @param supOrganization the supOrganization to set
	 */
	public void setSupOrganization(Organization supOrganization) {
		this.supOrganization = supOrganization;
	}

	/**
	 * @return the subOrganization
	 */
	public List<Organization> getSubOrganization() {
		return subOrganization;
	}

	/**
	 * @param subOrganization the subOrganization to set
	 */
	public void setSubOrganization(List<Organization> subOrganization) {
		this.subOrganization = subOrganization;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Organization() {
		super();
	}

	public Organization(String code, String desription, String name,
	                    List<Organization> subOrganization, Organization supOrganization, String type) {
		super();
		this.orgCode = code;
		this.desription = desription;
		this.name = name;
		this.subOrganization = subOrganization;
		this.supOrganization = supOrganization;
		this.type = type;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
}
